<!--
 * @Description: 
 * @Autor: lily
 * @Date: 2021-04-26 11:08:59
 * @LastEditors: lily
 * @LastEditTime: 2021-09-01 11:10:02
-->
<template>
  <base-modal
    title="新建应用"
    class="apply-inforn-dialog"
    :show.sync="visible"
    width="400px"
  >
    <div class="base-modal-content">
      <el-form :model="form" :rules="rules" ref="form" label-position="top">
        <el-form-item label="应用名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入应用名称"
            maxlength="20"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false" :disabled="pending">关闭</el-button>
      <el-button type="primary" @click="onCreatApply" :disabled="pending">保存</el-button>
    </div>
  </base-modal>
</template>
<script>
/* api */
import * as PassApi from '@src/api/PassApi';
import { openAccurateTab } from '@src/util/platform'
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
export default {
  name:'apply-inforn',
  data() {
    let validateTypeName = (rule, value, callback) => {
      if (value === '') return callback(new Error('请输入应用名称'));
      if(value.length > 20) callback(new Error('名称不能超过20个字'));
      if(!/^[a-zA-Z0-9\u4e00-\u9fa5]+$/.test(value)) return callback(new Error('请输入中文、字母、数字'));

      callback();
    };
    return{
      token:'',
      pending: false,
      visible: false,
      form:{
        name:''
      },
      rules: {
        name: [
          { required: true, message: '请输入应用名称', trigger: 'blur' },
          { validator: validateTypeName, trigger: 'blur' }
        ]
      },
        
            
    }
  },
  methods: {
    open() {
      this.form.name = '';
      this.visible = true;
    },
    /**
     * @description: 创建应用
     * @param {*}
     */
    async onCreatApply() {
      const validateRes = await this.$refs.form.validate();
      if (!validateRes) return;
      
      if(!this.form.name) return this.$message.warning('请输入应用名称!');
      this.pending = true;

      PassApi.creatInsert(this.form).then(res=>{
        const {code, data, message} = res;
        this.pending = false;
        if(code == 0) {
          openAccurateTab({
            type: PageRoutesTypeEnum.PagePaasSettingManager,
            titleKey: data.name,
            params: `appId=${data.appId}`
          });
          this.visible = false;
          this.$emit('close');
        }else{
          this.$message.warning(message)
        }
      }).catch(error=>{console.log(error)})

    }
  }
    
}
</script>
<style lang="scss" scoped>
.apply-inforn-dialog{
    .base-modal-content{
        padding: 20px;
    }
    
}
</style>